package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.enums.OrderDeleteEnum;
import com.member.common.utils.DateUtils;
import com.member.common.utils.StringUtils;
import com.member.shop.domain.SysBizShopSpecs;
import com.member.shop.mapper.SysBizShopSpecsMapper;
import com.member.shop.service.ISysBizShopSpecsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 商品规格Service业务层处理
 *
 * @author lixinchen
 * @date 2024-12-29
 */
@Service
public class SysBizShopSpecsServiceImpl extends ServiceImpl<SysBizShopSpecsMapper, SysBizShopSpecs> implements ISysBizShopSpecsService {
    @Resource
    private SysBizShopSpecsMapper sysBizShopSpecsMapper;

    /**
     * 查询商品规格
     *
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public SysBizShopSpecs selectSysBizShopSpecsById(Long id) {
        return sysBizShopSpecsMapper.selectById(id);
    }

    /**
     * 查询商品规格列表
     *
     * @param queryWrapper 商品规格
     * @return 商品规格
     */
    @Override
    public List<SysBizShopSpecs> selectSysBizShopSpecsList(LambdaQueryWrapper<SysBizShopSpecs> queryWrapper) {
        return sysBizShopSpecsMapper.selectList(queryWrapper);
    }

    /**
     * 查询商品规格列表
     *
     * @param sysBizShopSpecs 商品规格
     * @return 商品规格
     */
    @Override
    public List<SysBizShopSpecs> selectSysBizShopSpecsList(SysBizShopSpecs sysBizShopSpecs) {
        return sysBizShopSpecsMapper.selectSysBizShopSpecsList(sysBizShopSpecs);
    }

    /**
     * 新增商品规格
     *
     * @param sysBizShopSpecs 商品规格
     * @return 结果
     */
    @Override
    public int insertSysBizShopSpecs(SysBizShopSpecs sysBizShopSpecs) {
        sysBizShopSpecs.setCreateTime(DateUtils.getNowDate());
        sysBizShopSpecs.setBizId(UUID.randomUUID().toString());
        if(sysBizShopSpecs.getParentId()!=null && sysBizShopSpecs.getParentId()!=0){
            SysBizShopSpecs sysBizShopSpecs1 = sysBizShopSpecsMapper.selectSysBizShopSpecsById(sysBizShopSpecs.getParentId());
            sysBizShopSpecs.setTenantId(sysBizShopSpecs1.getTenantId());

        }
        return sysBizShopSpecsMapper.insert(sysBizShopSpecs);
    }

    /**
     * 修改商品规格
     *
     * @param sysBizShopSpecs 商品规格
     * @return 结果
     */
    @Override
    public int updateSysBizShopSpecs(SysBizShopSpecs sysBizShopSpecs) {
        sysBizShopSpecs.setUpdateTime(DateUtils.getNowDate());
        return sysBizShopSpecsMapper.updateById(sysBizShopSpecs);
    }

    /**
     * 批量删除商品规格
     *
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Override
    public int deleteSysBizShopSpecsByIds(Long[] ids) {
        return sysBizShopSpecsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除商品规格信息
     *
     * @param id 商品规格主键
     * @return 结果
     */
    @Override
    public int deleteSysBizShopSpecsById(Long id) {
        SysBizShopSpecs sysBizShopSpecs = new SysBizShopSpecs();
        sysBizShopSpecs.setId(id);
        sysBizShopSpecs.setDeleted((long) OrderDeleteEnum.DELETE.getKey());
        return sysBizShopSpecsMapper.updateById(sysBizShopSpecs);
    }

    /**
     * 是否存在目录子节点
     *
     * @param id 目录ID
     * @return 结果
     */
    @Override
    public boolean hasChildById(Long id) {
        int result = sysBizShopSpecsMapper.hasChildById(id);
        return result > 0;
    }
}
